package com.yuesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.CustomException;
import com.yuesystem.common.R;
import com.yuesystem.dto.responsebody.LogResponseBody;
import com.yuesystem.entity.AcademyItem;
import com.yuesystem.service.AcademyItemService;
import com.yuesystem.service.ChangeItem1Service;
import com.yuesystem.service.ChangeItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 修改条目类.
 */
@RestController
@CrossOrigin
@RequestMapping("/changes")
@Slf4j
public class ChangeItemController {
  @Autowired
  private ChangeItemService changeItemService;

  @Autowired
  private ChangeItem1Service changeItem1Service;

  @Autowired
  private AcademyItemService academyItemService;


  /**
   * 新增编辑条目.
   *
   * @param academyItem 书院条目类
   * @return 新增成功与否的提示信息
   */
  @PostMapping
  public R<String> add(@RequestBody AcademyItem academyItem) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status == 0) {
      throw new CustomException("您未获得该权限！");
    }
    changeItemService.addItem(academyItem);
    return R.success("添加条目成功！");
  }


  /**
   * 通过某个书院条目ID删除某个条目.
   *
   * @param itemId 书院条目id
   * @return 删除成功与否的提示信息
   */
  @DeleteMapping("/{itemId}")
  public R<String> delete(@PathVariable Long itemId) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status == 0) {
      throw new CustomException("您未获得该权限！");
    }
    // 根据书院获得具体的书院条目信息
    AcademyItem academyitem = academyItemService.getById(itemId);

    if (academyitem == null || academyitem.getIsDeleted() == 1) {
      return R.error("该条目不存在，请刷新后重试！");
    }

    changeItemService.deleteItem(academyitem);
    return R.success("删除条目成功！");
  }

  /**
   * 修改官职条目信息.
   *
   * @param  academyItem 书院条目类
   * @return 修改成功与否的提示信息
   */
  @PutMapping
  public R<String> update(@RequestBody AcademyItem academyItem) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status == 0) {
      throw new CustomException("您未获得该权限！");
    }

    changeItemService.updateItem(academyItem);

    return R.success("修改条目成功！");
  }

  /**
   * 查询个人日志信息,分页显示.
   *
   * @param startTime 起始时间
   * @param endTime 结束时间
   * @param operation 操作
   * @param academyName 书院名
   * @return 日志分页
   */
  @GetMapping("/myLog")
  public R<Page> getMyLog(@RequestParam int page, @RequestParam int pageSize,
                          @RequestParam String startTime, @RequestParam String endTime,
                          @RequestParam String operation, @RequestParam String academyName) {
    // 得到个人ID
    Long userId = BaseContext.getCurrentId();

    // 调用service层的方法
    Page<LogResponseBody> pageInfo = changeItem1Service.getPage(userId, page, pageSize, startTime,
        endTime, operation, academyName);

    return R.success(pageInfo);
  }


  /**
   * 分页查询所有人日志.
   *
   * @param page 起始页
   * @param pageSize 页数
   * @param startTime 起始时间
   * @param endTime 结束时间
   * @param name 用户名
   * @param operation 操作
   * @param academyName 书院名
   * @return 日志分页
   */
  @GetMapping("/allLog")
  public R<Page> getAllLog(@RequestParam int page, @RequestParam int pageSize,
                           @RequestParam String startTime, @RequestParam String endTime,
                           @RequestParam String name, @RequestParam String operation,
                           @RequestParam String academyName) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status != 2) {
      throw new CustomException("您还不是管理员，无法查看所有人的日志！");
    }

    // 调用service层的方法
    Page<LogResponseBody> pageInfo = changeItem1Service.getAllPage(name, page, pageSize, startTime,
        endTime, operation, academyName);
    return R.success(pageInfo);
  }


}
